﻿using System;
using System.Collections.Generic;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary
{
    public class EmployeeModule
    {
        private readonly EmployeeDAO _dao = new EmployeeDAO();

        public bool CheckEmployeeEmail(string email)
        {
            return _dao.CheckEmployeeEmail(email);
        }

        public bool AddEmployee(string name, bool sex, DateTime birthday, string position, string phone, string email,
                                string address, string city, string country, string jobtitle, byte[] image)
        {
            string err;
            if (validData(position, jobtitle, phone, email, out err))
            {
                var employee = new Employee
                                   {
                                       EmployeeName = name,
                                       EmployeeSex = sex,
                                       EmployeeBirthday = birthday,
                                       EmployeePosition = position,
                                       EmployeePhone = phone,
                                       EmployeeEmail = email,
                                       EmployeeAddress = address,
                                       EmployeeCity = city,
                                       EmployeeCountry = country,
                                       EmployeeJobTitle = jobtitle,
                                       EmployeeImage = image,
                                       EmployeeAccount = Utility.CreateAccount(name),
                                       EmployeePassword = Utility.CreateRandomPassword(6),
                                       EmployeeAvailable = true
                                   };
                return _dao.InsertEmployee(employee);
            }
            throw new Exception(err);
        }


        public bool ModifyEmployee(int employeeID, string name, bool sex, DateTime birthday, string position,
                                   string phone, string email, string address, string city, string country,
                                   string jobtitle, byte[] image)
        {
            string err;
            if (validData(position, jobtitle, phone, email, out err))
            {
                var employee = new Employee
                                   {
                                       EmployeeID = employeeID,
                                       EmployeeName = name,
                                       EmployeeSex = sex,
                                       EmployeeBirthday = birthday,
                                       EmployeePosition = position,
                                       EmployeePhone = phone,
                                       EmployeeEmail = email,
                                       EmployeeAddress = address,
                                       EmployeeCity = city,
                                       EmployeeCountry = country,
                                       EmployeeJobTitle = jobtitle,
                                       EmployeeImage = image,
                                       EmployeeAvailable = true
                                   };
                return _dao.UpdateEmployee(employee);
            }
            throw new Exception(err);
        }

        public bool DeactiveEmployee(int employeeID)
        {
            return _dao.DeactiveEmployee(employeeID);
        }

        public List<vEmployeeSubDetail> GetEmployees(bool all)
        {
            return _dao.SelectEmployees(all);
        }

        public Employee GetEmployee(int employeeID)
        {
            Employee employee = _dao.SelectEmployee(employeeID);
            return employee;
        }

        public List<vEmployeeSubDetail> GetEmployees(string employeeName)
        {
            return _dao.SelectEmployees(employeeName);
        }

        private bool validData(string position, string jobtitle, string phone, string newemail, out string err)
        {
            if (!Utility.validPhoneEmail(phone, newemail, out err))
            {
                return false;
            }

            const string lstJobTitle = "Junior Senior";

            if (jobtitle == null || lstJobTitle.IndexOf(jobtitle) == -1)
            {
                err = "Job title must be Junior, Senior";
                return false;
            }

            const string lstPosition = "Cleaner Sales Recipient Manager Web";

            if (position == null || lstPosition.IndexOf(position) == -1)
            {
                err = "Position must be Cleaner, Sales, Reception, Manager";
                return false;
            }

            err = "";
            return true;
        }
    }
}